package com.example.meituan.activity.bean;

import android.database.Cursor;

import java.util.ArrayList;

public class OrderBean {
    // 数据库字段名常量，便于维护
    public static final String TABLE_NAME = "d_orders";
    public static final String COLUMN_S_ID = "s_id";
    public static final String COLUMN_S_ORDER_TIME = "s_order_time";
    public static final String COLUMN_S_BUSINESS_ID = "s_business_id";
    public static final String COLUMN_S_USER_ID = "s_user_id";
    public static final String COLUMN_S_ORDER_STA = "s_order_sta";
    public static final String COLUMN_S_ORDER_ADDRESS = "s_order_address";
    public static final String COLUMN_S_ORDER_DETIALS_ID = "s_order_detials_id";

    private String sId;
    private String sOrderTime;
    private String sBusinessId;
    private String sUserId;
    private String sOrderSta;
    private String sOrderAddress;
    private String sOrderDetialsId;

    // 默认构造函数
    public OrderBean() {}

    // 带参数的构造函数
    public OrderBean(String sId, String sOrderTime, String sBusinessId, String sUserId, String sOrderSta, String sOrderAddress, String sOrderDetialsId) {
        this.sId = sId;
        this.sOrderTime = sOrderTime;
        this.sBusinessId = sBusinessId;
        this.sUserId = sUserId;
        this.sOrderSta = sOrderSta;
        this.sOrderAddress = sOrderAddress;
        this.sOrderDetialsId = sOrderDetialsId;
    }

    // Getter 和 Setter 方法
    public String getsId() { return sId; }
    public void setsId(String sId) { this.sId = sId; }

    public String getsOrderTime() { return sOrderTime; }
    public void setsOrderTime(String sOrderTime) { this.sOrderTime = sOrderTime; }

    public String getsBusinessId() { return sBusinessId; }
    public void setsBusinessId(String sBusinessId) { this.sBusinessId = sBusinessId; }

    public String getsUserId() { return sUserId; }
    public void setsUserId(String sUserId) { this.sUserId = sUserId; }

    public String getsOrderSta() { return sOrderSta; }
    public void setsOrderSta(String sOrderSta) { this.sOrderSta = sOrderSta; }

    public String getsOrderAddress() { return sOrderAddress; }
    public void setsOrderAddress(String sOrderAddress) { this.sOrderAddress = sOrderAddress; }

    public String getsOrderDetialsId() { return sOrderDetialsId; }
    public void setsOrderDetialsId(String sOrderDetialsId) { this.sOrderDetialsId = sOrderDetialsId; }

    // 将 OrderBean 转换为适合插入数据库的参数数组
    public Object[] toInsertParams() {
        return new Object[]{sId, sOrderTime, sBusinessId, sUserId, sOrderSta, sOrderAddress, sOrderDetialsId};
    }

    // 从 Cursor 中填充 OrderBean
    public static OrderBean fromCursor(Cursor cursor) {
        if (cursor != null && cursor.moveToFirst()) {
            return new OrderBean(
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_ID)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_ORDER_TIME)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_BUSINESS_ID)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_USER_ID)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_ORDER_STA)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_ORDER_ADDRESS)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_S_ORDER_DETIALS_ID))
            );
        }
        return null;
    }

    // 从 Cursor 集合中批量创建 OrderBean 列表
    public static ArrayList<OrderBean> fromCursorList(Cursor cursor) {
        ArrayList<OrderBean> orders = new ArrayList<>();
        if (cursor != null && cursor.moveToFirst()) {
            do {
                orders.add(fromCursor(cursor));
            } while (cursor.moveToNext());
        }
        return orders;
    }

    @Override
    public String toString() {
        return "OrderBean{" +
                "sId='" + sId + '\'' +
                ", sOrderTime='" + sOrderTime + '\'' +
                ", sBusinessId='" + sBusinessId + '\'' +
                ", sUserId='" + sUserId + '\'' +
                ", sOrderSta='" + sOrderSta + '\'' +
                ", sOrderAddress='" + sOrderAddress + '\'' +
                ", sOrderDetialsId='" + sOrderDetialsId + '\'' +
                '}';
    }
}
